package com.goldcn.classify.service;

import com.goldcn.classify.dao.ClassifyAttrDefMapper;
import com.goldcn.classify.dao.RefPlatformMapper;
import com.goldcn.classify.model.ClassifyAttrDef;
import com.goldcn.classify.model.RefPlatformKey;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/3/13.
 */
@Service
public class PlatformService {

    @Autowired
    private ClassifyAttrDefMapper classifyAttrDefMapper;
    @Autowired
    private RefPlatformMapper refPlatformMapper;

    private Logger logger=Logger.getLogger(PlatformService.class);

    public Object removePlatform(){
        //查询所有属性
        List<ClassifyAttrDef> defs=classifyAttrDefMapper.selectAllAttr();

        for(ClassifyAttrDef def:defs){
            RefPlatformKey key=new RefPlatformKey();
            key.setRefID(def.getCfadID());
            if(def.getClazzPlatform().equals("0")){
                key.setPlatformID(1);
                this.insert(key,def.getClazzAttrName());
                key.setPlatformID(2);
                this.insert(key,def.getClazzAttrName());
                key.setPlatformID(3);
                this.insert(key,def.getClazzAttrName());
                key.setPlatformID(5);
                this.insert(key,def.getClazzAttrName());
                key.setPlatformID(6);
                this.insert(key,def.getClazzAttrName());
            }else{
                key.setPlatformID(Integer.parseInt(def.getClazzPlatform()));
                this.insert(key,def.getClazzAttrName());
            }
        }
        return "success";
    }

    public List<Integer> queryPlatformByRef(String refid){
        return refPlatformMapper.selectPlatformByRefID(refid);
    }

    public void insert(String refID,Integer platform,String name){
        RefPlatformKey key=new RefPlatformKey();
        key.setRefID(refID);
        key.setPlatformID(platform);
        int result=refPlatformMapper.insert(key);
        if(result==0){
            logger.error(name+" insert error");
        }
    }

    public void deleteByResID(String refID) {
        RefPlatformKey key=new RefPlatformKey();
        key.setRefID(refID);
        refPlatformMapper.deleteByPrimaryKey(key);
    }

    private void insert(RefPlatformKey key,String name){
        int result=refPlatformMapper.insert(key);
        if(result==0){
            logger.error(name+" remove error");
        }
    }



}
